<template>
  <div class="container">
<!--    发表新评论-->
    <div v-if="userInfo.id !== -1">
      <el-row :gutter="20">
        <el-col :span="2">
          <img class="avatar" :src="userInfo.avatar" width="36" height="36"/>

        </el-col>
        <el-col :span="18">
          <el-input
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
              placeholder="发表评论"
              v-model="inputComment"
          >
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" style="background: #1bb6e2; "  @click="commitComment(-1)">
            <p style="line-height: 16px">发表</p>
            评论
          </el-button>
        </el-col>
      </el-row>
    </div>
<!--    登录后发表评论-->
    <div style="margin:0 5%;" v-else>
      <el-row :gutter="20">
        <el-col :span="2">
          <img class="avatar" :src="defaultAvatar" width="36" height="36"/>

        </el-col>
        <el-col :span="18">
          <el-input
              disabled
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4}"
              placeholder="请先登录后发表评论"
              >
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" style="background: #1bb6e2; " @click="loginHandle">
            登录
          </el-button>
        </el-col>
      </el-row>
    </div>
<!--    分割-->
    <el-divider></el-divider>

    <div class="comment" v-for="item in comments" :key="item.id">
<!--    用户信息-->
      <div class="info">
        <img class="avatar" :src="item.avatar" width="36" height="36"/>
        <div class="right">
          <div class="name">{{item.userName}}</div>
          <div class="date">{{item.createTime}}</div>
        </div>
      </div>
<!--      评论内容-->
      <div class="content">{{item.content}}</div>
<!--      点赞量以及回复操作-->
      <div class="control">
        <span class="like" :class="{active: item.isLike}" @click="likeClick(item)">
          <i class="iconfont icon-like"></i>
          <span class="like-num">{{item.likeNum > 0 ? item.likeNum + '人赞' : '赞'}}</span>
        </span>
        <span class="comment-reply" @click="showCommentInput(item)">
          <i class="iconfont icon-comment"></i>
          <span style="margin-right: 25px">回复 </span>
        </span>
<!--        回复量-->
        <i class="el-icon-chat-dot-square"></i>&nbsp;
        {{item.children.total}}
      </div>
<!--    子评论 -->
      <div class="reply">
        <div class="item" v-for="reply in item.children.children.slice(pageNum-1,pageSize)" :key="reply.id">


          <div class="reply-content">
            <img class="avatar" style="margin-right:3px;border-radius: 50%;border:1px solid gray;" :src="reply.avatar" width="36" height="36"/>
            <span class="from-name">{{reply.userName}}</span><span>: </span>
            <span class="to-name">@{{reply.toCommentUserName}}</span>
            <span>{{reply.content}}</span>
          </div>
          <div class="reply-bottom">
            <span>{{reply.createTime}}</span>
            <span class="reply-text" @click="showCommentInput(item, reply)">
              <i class="iconfont icon-comment"></i>
              <span>回复</span>
            </span>
          </div>
        </div>
<!--        v-if= "reply.length > 0"-->
<!--        <div class="write-reply"  @click="showCommentInput(item)">-->
<!--          <i class="el-icon-edit"></i>-->
<!--          <span class="add-comment">添加新评论</span>-->
<!--        </div>-->
        <transition name="fade">
          <div class="input-wrapper" v-if="showItemId === item.id">
            <el-input class="gray-bg-input"
                      v-model="inputComment"
                      type="textarea"
                      :rows="3"
                      autofocus
                      :placeholder="inputReplace">
            </el-input>
            <div class="btn-control">
              <span class="cancel" @click="cancel">取消</span>
              <el-button class="btn" type="success" round @click="commitComment(item)">确定</el-button>
            </div>
          </div>
        </transition>
      </div>
    </div>

  </div>
</template>

<script>

import Vue from 'vue'
import {reqAddComment} from "@/API";

export default {
  name:"commentCom",
  props: {
    comments: {
      type: Array,
      required: true
    },
    userInfo:{
      type: Object
    },
    articleId:{
      type: Number
    }
  },
  components: {},
  data() {
    return {
      //新增评论数据
      inputReplace:'',
      inputComment: '',
      showItemId: '',
      //被回复评论相关信息
      toItem:{},
      defaultAvatar:require("@/assets/img/comment/null.jpg"),
      //子评论刷新
      pageNum:1,
      pageSize:5
    }
  },
  computed: {},
  mounted() {
    console.log(this.comments)
  },
  methods: {
    //跳转登录
    loginHandle(){
      this.$router.push({
        path:"/login",
        query: {
          redirect: this.$router.currentRoute.fullPath // router对象自带的属性
        }
      })
    },

    /**
     * 点赞
     */
    likeClick(item) {
      if (item.isLike === null) {
        Vue.$set(item, "isLike", true);
        item.likeNum++
      } else {
        if (item.isLike) {
          item.likeNum--
        } else {
          item.likeNum++
        }
        item.isLike = !item.isLike;
      }
    },

    /**
     * 点击取消按钮
     */
    cancel() {
      this.showItemId = ''
    },

    /**
     * 提交评论
     */
    commitComment(type) {

      if (typeof this.comments == 'string' && this.comments.length > 0) {
        this.$message.warning("评论不能为空！")

      }
      else{

        let data;
        // console.log(item,this.toItem)
        if(type === -1 ){

          console.log(this.comments)

          data = {
            articleId:this.articleId,
            type:0,
            rootId:-1,
            toCommentId: -1,
            toCommentUserId: -1,
            content:this.inputComment

          }
        }else{

          data = {
            articleId:this.toItem.articleId,
            type:0,
            rootId:this.toItem.rootId === "-1" ? this.toItem.id : this.toItem.rootId,
            toCommentId: this.toItem.id,
            toCommentUserId: this.toItem.createBy,
            content:this.inputComment

          }
          // this.inputComment.substring(this.inputComment.indexOf(" "))
          console.log(data)
        }

        //新增评论
        reqAddComment(data).then((res) =>{
          if(res.status === 200){
            this.$message.success("评论成功")
            //刷新页面
            location.reload()
            // this.$forceUpdate()
          }else if(res.status === 507){
            this.$message.warning("评论不能为空")
          }else{

            this.$message.warning("请先登录账号！")
            this.$router.push({
              path:"/login",
              query: {
                redirect: this.$router.currentRoute.fullPath // router对象自带的属性
              }
            })
          }
        })

      }


      // console.log(this.inputComment);
    },

    /**
     * 点击评论按钮显示输入框
     * item: 当前大评论
     * reply: 当前回复的评论
     */
    showCommentInput(item, reply) {
      if (reply) {
        this.inputReplace = "回复 @" + reply.userName + " :"
        this.toItem = reply
      } else {
        this.inputComment = ''
        this.toItem = item
      }
      //评论框的显示隐藏
      this.showItemId = item.id

    }
  },
  created() {
  }
}
</script>

<style scoped lang="scss">

@import "@/assets/scss/index";

.avatar {
  border-radius: 50%;
  border:1px solid #cbc9c9;
}

.container {
  padding: 0 10px;
  box-sizing: border-box;
  .comment {
    display: flex;
    flex-direction: column;
    padding: 10px;
    border-bottom: 1px solid $border-fourth;
    .info {
      display: flex;
      align-items: center;
      .avatar {
        border-radius: 50%;
        border:1px solid #cbc9c9;
      }
      .right {
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        .name {
          font-size: 16px;
          color: $text-main;
          margin-bottom: 5px;
          font-weight: 500;
        }
        .date {
          font-size: 12px;
          color: $text-minor;
        }
      }
    }
    .content {
      font-size: 16px;
      color: $text-main;
      line-height: 20px;
      padding: 10px 0;
    }
    .control {
      display: flex;
      align-items: center;
      font-size: 14px;
      color: $text-minor;
      .like {
        display: flex;
        align-items: center;
        margin-right: 20px;
        cursor: pointer;
        &.active, &:hover {
          color: $color-main;
        }
        .iconfont {
          font-size: 14px;
          margin-right: 5px;
        }
      }
      .comment-reply {
        display: flex;
        align-items: center;
        cursor: pointer;
        &:hover {
          color: $text-333;
        }
        .iconfont {
          font-size: 16px;
          margin-right: 5px;
        }
      }

    }
    .reply {
      margin: 10px 0;
      border-left: 2px solid $border-first;
      .item {
        margin: 0 10px;
        padding: 10px 0;
        border-bottom: 1px dashed $border-third;
        .reply-content {
          display: flex;
          align-items: center;
          font-size: 14px;
          color: $text-main;
          .from-name {
            color: $color-main;
          }
          .to-name {
            color: $color-reply;
            margin-left: 5px;
            margin-right: 5px;
          }
        }
        .reply-bottom {
          display: flex;
          align-items: center;
          margin-top: 6px;
          font-size: 12px;
          color: $text-minor;
          .reply-text {
            display: flex;
            align-items: center;
            margin-left: 10px;
            cursor: pointer;
            &:hover {
              color: $text-333;
            }
            .icon-comment {
              margin-right: 5px;
            }
          }
        }
      }
      .write-reply {
        display: flex;
        align-items: center;
        font-size: 14px;
        color: $text-minor;
        padding: 10px;
        cursor: pointer;
        &:hover {
          color: $text-main;
        }
        .el-icon-edit {
          margin-right: 5px;
        }
      }
      .fade-enter-active, fade-leave-active {
        transition: opacity 0.5s;
      }
      .fade-enter, .fade-leave-to {
        opacity: 0;
      }
      .input-wrapper {
        padding: 10px;
        .gray-bg-input, .el-input__inner {
          /*background-color: #67C23A;*/
        }
        .btn-control {
          display: flex;
          justify-content: flex-end;
          align-items: center;
          padding-top: 10px;
          .cancel {
            font-size: 16px;
            color: $text-normal;
            margin-right: 20px;
            cursor: pointer;
            &:hover {
              color: $text-333;
            }
          }
          .confirm {
            font-size: 16px;
          }
        }
      }
    }
  }
}
</style>
